//
//  MeatOrderApi.m
//  PigMarket
//
//  Created by 张敬 on 2019/9/10.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatOrderApi.h"
#import "Request.h"
#import "RequestHandler.h"
#import "NetworkConfigration.h"
#import "SystemUtil.h"


@implementation MeatOrderApi


+ (instancetype)sharedInstance {
    
    static MeatOrderApi *sharedInstance = nil;
    static dispatch_once_t token;
    
    dispatch_once(&token,^{
        if(sharedInstance == nil){
            sharedInstance = [[MeatOrderApi alloc] init];
        }
    } );
    
    return sharedInstance;
}


#pragma mark - 订单列表
- (RACSignal<MeatOrderListModel *> *)getOrderListByParams:(MeatOrderParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMine_MyOrder];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [MeatOrderListModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 订单取消
- (RACSignal<NSNull *> *)getOrderCancelByGoodId:(NSNumber *)goodsOrderId reason:(NSString *)cancelReason{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMine_MyOrderCancel];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"goodsOrderId":goodsOrderId, @"cancelReason":cancelReason };
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}

#pragma mark - 朋友代付
- (RACSignal <MeatOrderFriendPayModel *> *)getOrderFriendPayByGoodId:(NSNumber *)goodsOrderId{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@", URLMine_MyOrderFriendPay, goodsOrderId];
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = [MeatOrderFriendPayModel class];
    return [self createSignalWithReuqest:request];
    
}

#pragma mark - 确认收货
- (RACSignal <NSNull *> *)getOrderConfirmOverByGoodId:(NSNumber *)goodsOrderId{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@", URLMine_MyOrderConfirmOver, goodsOrderId];
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
    
}


#pragma mark - 删除订单
- (RACSignal <NSNull *> *)getOrderDeleteByGoodId:(NSNumber *)goodsOrderId{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@", URLMine_MyOrderDelete, goodsOrderId];
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
    
}


#pragma mark - 发表评价
- (RACSignal<MeatOrderCommentModel *> *)getOrderEvaluateByParams:(MeatOrderCommentParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMine_MyOrderEvaluate];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [MeatOrderCommentModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 追加评论
- (RACSignal<NSNull *> *)getOrderEvaluateAppendByParams:(MeatOrderCommentAppendParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMine_MyOrderAppendEvaluate];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}

#pragma mark - 各个状态下的订单数量
- (RACSignal <MeatOrderCommentModel *> *)getOrderStateCount{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMine_MyOrderStatusCount];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = [MeatOrderCommentModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 评价明细
- (RACSignal<MeatOrderCommentDetailModel *> *)getOrderEvaluateDetailByParams:(MeatOrderCommentDetailParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMine_MyOrderEvaluateDetail];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [MeatOrderCommentDetailModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 点赞/取消点赞评论 //type 类型：1点赞2取消点赞
- (RACSignal <NSNull *> *)getOrderCommentLikeOrUnLikeById:(NSNumber *)orderEvaluateId type:(NSNumber *)type{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@/%@", URLMine_MyOrderLikeOrUnLike, orderEvaluateId, kMemberId, type];
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
    
}


#pragma mark - 评价成功
- (RACSignal<MeatOrderEvaluationSuccessModel *> *)getOrderEvaluationByGoodId:(NSNumber *)goodsOrderId shopId:(NSString *)shopId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMine_MyOrderEvaluateSuccess];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"goodsOrderId":goodsOrderId, @"shopId":shopId, @"memberId":kMemberId };
    request.responseType = [MeatOrderEvaluationSuccessModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 订单详情
- (RACSignal<MeatOrderDetailModel *> *)getOrderDetailByParams:(MeatOrderDetailParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMine_MyOrderDetails];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [MeatOrderDetailModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 是否是服务商
- (RACSignal<ServiceModel *> *)getMineServiceIs{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMine_MyServiceIs];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId" :kMemberId};
    request.responseType = [ServiceModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 申请成为服务商 //1.线上2.线下3.取消
- (RACSignal<ServiceModel *> *)getOrderDetailByType:(NSNumber *)type{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMine_MyServiceApply];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId" :kMemberId, @"type":type};
    request.responseType = [ServiceModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 调查问卷列表
- (RACSignal <QuestionModel *> *)getQuestionList{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@", URLMine_MyQuestionList, kMemberId];
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = [QuestionModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 调查问卷提交
- (RACSignal<NSNull *> *)applyQuestionsByParams:(QuestionParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMine_MyQuestionApply];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 干点活赚点钱豚倦奖励与验证
- (RACSignal <TaskModel *> *)getTaskCheck{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@", URLMine_MyTaskCheck, kMemberId];
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = [TaskModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - Other

- (NSString *)getFullURLString:(NSString *)URLString {
    NetworkConfigration *configration = [NetworkConfigration defaultNetworkConfiguration];
    return [NSString stringWithFormat:@"%@%@", configration.baseApiURL, URLString];
}


- (RACSignal *)createSignalWithReuqest:(Request *) request {
    
    
    return [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        RequestHandler *handler = [[RequestHandler alloc] initWithRequest:request responseCallback:^(Response *response) {
            
            
#pragma mark - 回参数
            if (response.code == ResultCodeSuccess) {
                
                if(request.responseType) {
                    [subscriber sendNext:response.data];
                } else {
                    [subscriber sendNext:[NSNull null]];
                }
                [subscriber sendCompleted];
                
            } else {
                if (response.code == ResultCodeDeviceChanged) {//更换设备
                    UserInfo *userInfo = [UserInfoDataProvider sharedInstance].userInfo;
                    userInfo.isLogin = NO;
                    [UserInfoDataProvider sharedInstance].userInfo = userInfo;
                    
                }
                NSError *error = [NSError errorWithDomain:@"com.naturalgrowth.pigmarket.ErrorDomain" code:response.code userInfo: @{ NSLocalizedDescriptionKey : response.msg }];
                [subscriber sendError:error];
            }
            
        }];
        
        [handler sendRequest];
        
        return [RACDisposable disposableWithBlock:^{
            
            [handler cancelRequest];
        }];
    }] subscribeOn:[RACScheduler scheduler]] ;
}



@end
